import Controller from './Controller';

export default class BooleanController extends Controller {

	constructor( parent, object, property,config) {
		if(config == null) config = {} ;
		let className = "boolean" ;
		if(config.className != null)
			className = config.className ;
		super( parent, object, property, className, 'label', config );

		//是否值取反，即false时选上，true不勾选的情形
		this.netate = config.negate ;
		if(this.netate !== true) this.netate = false ;

		this.$input = document.createElement( 'input' );
		this.$input.setAttribute( 'type', 'checkbox' );
		this.$input.setAttribute( 'aria-labelledby', this.$name.id );

		let checkboxClass = null ;
		if(config.checkboxClass != null)
			checkboxClass = config.checkboxClass ;
		if(checkboxClass != null && checkboxClass != "")
			this.$input.classList.add(checkboxClass) ;

		this.$widget.appendChild( this.$input );

		let label = object[property+"_label"] ;
		if(label != null && label != ""){
			this.$span = document.createElement("span") ;
			this.$span.classList.add( 'checkboxSpan' );
			this.$span.innerText = label ;
			this.$widget.appendChild( this.$span );
		}

		this.$input.addEventListener( 'change', () => {
			if(this.netate === true)
				this.setValue( !this.$input.checked );
			else
				this.setValue( this.$input.checked );
			this._callOnFinishChange();
		} );

		this.$disable = this.$input;

		this._appendEndDomHtml(config.endHtml) ;
		// let endHtml = config.endHtml ;
		// if(endHtml != null && endHtml != ""){
		// 	this.$span = document.createElement( 'span' );
		// 	this.$span.classList.add("lilInlineEndSpan") ;
		// 	this.$widget.appendChild( this.$span );
		//
		// 	this.$span.innerHTML = endHtml ;
		// }


		this.updateDisplay();

	}

	updateDisplay() {
		if(this.netate === true)
			this.$input.checked = !this.checkBoolean(this.getValue(),false) ;
		else
			this.$input.checked = this.checkBoolean(this.getValue(),false) ;
		return this;
	}

}
